package com.sky.zzh.devicemanager;

import org.json.JSONObject;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.next.intf.ITaskListener;
import com.next.net.SHPost;
import com.next.net.SHTask;
import com.sky.widget.SHDialog;
import com.sky.widget.SHToast;
import com.sky.zzh.BaseFragment;
import com.sky.zzh.R;
import com.sky.zzh.util.CommonUtil;
import com.sky.zzh.util.ConfigDefinition;
import com.sky.zzh.util.UserInfoManager;
/**
 * 重命名设备
 * @author skypan
 *
 */
public class RenameFragment extends BaseFragment implements ITaskListener{

	private EditText mEtName;
	private Button mBtnSave;
	private SHPost updateTask;
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		mEtName = (EditText) view.findViewById(R.id.et_name);
		mBtnSave = (Button) view.findViewById(R.id.btn_right);
		mEtName.setText(getActivity().getIntent().getStringExtra("name"));
		mBtnSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(CommonUtil.isEmpty(mEtName.getText().toString().trim())){
					SHToast.showToast(getActivity(), "名称不能为空");
					return;
				}
				SHDialog.ShowProgressDiaolg(getActivity());
				updateTask = new SHPost();
				updateTask.setUrl(ConfigDefinition.URL+"user/RegisterUserDevice");
				updateTask.setListener(RenameFragment.this);
				updateTask.getTaskArgs().put("UserID", UserInfoManager.getInstance().getUserId());
				updateTask.getTaskArgs().put("UUID", getActivity().getIntent().getStringExtra("UUID"));
				updateTask.getTaskArgs().put("Description", mEtName.getText().toString().trim());
				updateTask.start();
			}
		});
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_rename, container, false);
		return view;
	}

	@Override
	public void onTaskFinished(SHTask task) throws Exception {
		// TODO Auto-generated method stub
		SHDialog.dismissProgressDiaolg();
		JSONObject json = new JSONObject(task.getResult().toString());
		if("200".equals(json.optString("ReturnCode"))){
			getActivity().finish();
		}
	}

	@Override
	public void onTaskFailed(SHTask task) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTaskUpdateProgress(SHTask task, int count, int total) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTaskTry(SHTask task) {
		// TODO Auto-generated method stub
		
	}

}
